package com.sunsy.authserver.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunsy.authserver.beans.dto.UserQueryDto;
import com.sunsy.authserver.beans.po.UniformUser;
import com.sunsy.authserver.beans.vo.AdminUserVO;
import com.sunsy.authserver.beans.vo.UniformUserVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * (UniformUser)表数据库访问层
 *
 * @author makejava
 * @since 2021-07-20 14:40:35
 */
@Mapper
public interface UniformUserDao extends BaseMapper<UniformUser> {

    @Select("select * from uniform_user where mobile = #{mobile}")
    UniformUser getUserByMobile(String mobile);

    @Select("select * from uniform_user where email = #{email}")
    UniformUser getUserByEmail(String email);

    @Select("<script>select * from uniform_user where email = #{email} or mobile = #{mobile} " +
            "<if test=\"domainAccount != null and domainAccount !=''\"> or domain_account = #{domainAccount}</if>" +
            "</script>")
    UniformUser getUserByInfo(UniformUser user);

    @Update("UPDATE uniform_user SET open_id=#{openId}, username=#{username}, password=#{password}, mobile=#{mobile}, email=#{email}, status=#{status}, dept=#{dept}, updated_by=#{updatedBy}, updated_time=#{updatedTime}, domain_account=#{domainAccount},deleted=0 WHERE id=#{id}")
    int updateByInsert(UniformUser user);

    @Select("select * from uniform_user where account = #{account}")
    UniformUser getUserByAccount(String account);

    @Select("select * from uniform_user where domain_account = #{domainAccount}")
    UniformUser getUserByDomain(String domainAccount);

    @Select("select user.* from uniform_user user,user_token ut where ut.user_id = user.id and ut.user_token = #{user_token}")
    UniformUserVO getUserByToken(String user_token);

    @Select("<script>" +
            "select u.*,us.status_name as statusName from uniform_user u ,user_status us" +
            "<if test=\"dto.tenant!=null and dto.tenant !=''\">,user_tenant_relation utr </if>" +
            "<if test=\"dto.appName!=null and dto.appName !=''\">,user_app_relation uar </if>" +
            "<where>" +
            "us.id = u.status and u.deleted  = false" +
            "<if test=\"dto.username!=null and dto.username !=''\"> and  u.username like concat('%',#{dto.username},'%') </if>" +
            "<if test=\"dto.tenant!=null and dto.tenant !=''\"> and  u.id = utr.user_id and utr.tenant_id  in (select id from tenant where name like concat('%',#{dto.tenant},'%')) </if>" +
            "<if test=\"dto.appName!=null and dto.appName !=''\"> and  u.id = uar.user_id and uar.app_id  in (select id from app_client where app_name like concat('%',#{dto.appName},'%')) </if>" +
            "<if test=\"dto.status!=null and dto.status !=''\"> and  u.status = #{dto.status} </if>" +
            "<if test=\"dto.mobile!=null and dto.mobile !=''\"> and  u.mobile like concat('%',#{dto.mobile},'%') </if>" +
            "<if test=\"dto.email!=null and dto.email !=''\"> and  u.email like concat('%',#{dto.email},'%') </if>" +
            "<if test=\"dto.dept!=null and dto.dept !=''\"> and  u.dept like concat('%',#{dto.dept},'%') </if>" +
            "</where>" +
            "</script>")
    IPage<AdminUserVO> getUserList(IPage<AdminUserVO> page, @Param("dto")UserQueryDto dto);


    @Select("select * from uniform_user")
    List<AdminUserVO> getAllUser();
}